package com.xhermit.learn.footprint.base.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xhermit.learn.footprint.base.domain.BaseEntity;
import com.xhermit.learn.footprint.base.mapper.BaseMapper;


 @Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class BaseServiceImpl<D extends BaseMapper<T>, T extends BaseEntity<T>> implements BaseService<T> {
	
	private static final int BATCH_OPERATION_COUNT=200;
	/**
	 * 持久层对象
	 */
	@Autowired
	protected D dao;

	@Override
	public T get(String id) {
		return dao.get(id);
	}

	@Override
	public List<T> findList(T model) {
		return dao.findList(model);
	}

	@Override
	public List<T> findAll() {
		return dao.findAll();
	}

	@Override
	public int getCount(T model) {
		return dao.getCount(model);
	}
	
 

	/***************************** 增删改操作 *****************************/
	
	@Transactional(readOnly = false)
	@Override
	public void insert(T entity) {
		entity.preInsert();
		dao.insert(entity);
	}
	
	@Transactional(readOnly = false)
	@Override
	public void insertBatch(List<T> list) {
		//批量提交的子列表
		List<T> subList=new ArrayList<T>();
		for (final T t : list) {
			t.preInsert();
			subList.add(t);
			//子列表已满,批量提交 
			if(subList.size() ==BATCH_OPERATION_COUNT){
				dao.insertBatch(subList);
				subList=new ArrayList<T>();
			}
		}
		//子列表未满的部分,做一次批量提交 
		if(subList.size() >0 && subList.size() <BATCH_OPERATION_COUNT){
		       dao.insertBatch(subList);
		}
	}

	@Transactional(readOnly = false)
	@Override
	public void save(T entity) {
		int result = 0;
		if (StringUtils.isBlank(entity.getUuid())){
			entity.preInsert();
			result =  dao.insert(entity);
		}else{
			entity.preUpdate();
			result =  dao.update(entity);
		}	
		if(result == 0){
			throw new RuntimeException("No saved records");
		}
	}
	
	@Transactional(readOnly = false)
	@Override
	public void update(T entity) {
		entity.preUpdate();
		int result = dao.update(entity);
		if(result == 0){
			throw new RuntimeException("No updated records");
		}
	}
	
	@Transactional(readOnly = false)
	@Override
	public void updateBatch(List<T> list) {
		//批量提交的子列表
		List<T> subList=new ArrayList<T>();
		for (final T t : list) {
			t.preUpdate();
			subList.add(t);
			//子列表已满,批量提交 
			if(subList.size() ==BATCH_OPERATION_COUNT){
				int result = dao.updateBatch(subList);
				if(result == 0){
					throw new RuntimeException("No updated records");
				}
				subList=new ArrayList<T>();
			}
		}
		//子列表未满的部分,做一次批量提交 
		if(subList.size() >0 && subList.size() <BATCH_OPERATION_COUNT){
			int result =   dao.updateBatch(subList);
			if(result == 0){
				throw new RuntimeException("No updated records");
			}
		}
	}

	@Transactional(readOnly = false)
	@Override
	public void delete(final String id) {
		int result = dao.delete(id);
		if(result == 0){
			throw new RuntimeException("Record not deleted, id="+id);
		}
	}
	
	@Transactional(readOnly = false)
	@Override
	public void deleteBatch(final String[] ids) {
		int result = dao.deleteBatch(ids);
		if(result == 0){
			throw new RuntimeException("Records not deleted, ids="+ids);
		}
	}
	
	@Transactional(readOnly = false)
	@Override
	public void deleteLogic(final String id) {
		final int result = dao.deleteLogic(id);
		if(result == 0){
			throw new RuntimeException("Record not deleted, id="+id);
		}
	}
	
	
}
